<template>
	<el-cascader
			:options="departments"
			v-model="currentValue"
			@active-item-change="handleItemChange"
			:props="props"
	></el-cascader>
</template>
<script>
	import Vue from 'vue'
	import ElementUI from 'element-ui'
	import request from 'winged/utils/axios'
	import AutoComplete from 'winged/components/wt-autocomplete'
	import Permissions from 'winged/utils/wt-permission'

	Vue.use(ElementUI);
	Vue.component(AutoComplete.name, AutoComplete);
	export default{
		name : 'wt-department-select',
		props:{
			paramsData:{
				type : Object,
				default : {}
			},
			field:{
				type : Object,
				default : {}
			},
			form:{
				type: Object,
				default : {}
			}
		},
		data(){
			return{
				currentValue:[this.form[this.field.firstOptionsParam],this.form[this.field.secondOptionsParam]],
				departments:[],
				firstParams:this.field.firstOptionsParam,
				sedParams:this.field.secondOptionsParam,
				props: {
					value: 'department_id',
					label:'name',
					children: 'sed_department'
				}
			}
		},
		watch : {
			'currentValue'(val, old) {
				this.form[this.firstParams] = val[0];
				this.form[this.sedParams] = val[1];
			},
		},
		methods:{
			handleItemChange(val){
				this.getdepartmentList(val[0])
			},
			getdepartmentList(id){
				if (id) {
					let dpt = this.finddpt(id);
					if (dpt.sed_department && dpt.sed_department.length>0){
						return;
					}
				}
				this.departmentList(id);
			},
			departmentList(id){
				let user_id = Permissions.getCurrentUserId();
				let url = this.field.paramsQuery.url;
				let transfer = this.field.paramsQuery.transfer;
				let method = this.field.paramsQuery.method;
				let params = this.field.paramsQuery.params;
				let value = this.field.paramsQuery.firstValue;
				let lable = this.field.paramsQuery.firstLable;
				let data = {
					'user_id': user_id.toString(),
					'parent_id':id?id.toString():'0',
					'page_index':'0',
					'page_size':'999'
				};
				let leve1 = false;
				if (id == undefined){
					leve1 = true;
				}
				request(url,method, data, transfer, (data)=> {
					if (typeof(data) == 'string') {
						data = JSON.parse(data);
					}else {
						data = data
					}
					let department_list = data[params];
					if (leve1){
						for (let a=0;a<department_list.length;a++){
							this.departments.push({
								name: department_list[a][lable],
								department_id: department_list[a][value],
								sed_department: []
							})
							if (this.currentValue[0] == department_list[a][this.firstParams]){
								this.getdepartmentList(this.currentValue[0])
							}
						}
					}
					else {
						if (department_list.length == 0){
							this.finddpt(id).sed_department.push({
								name: this.finddpt(id)[lable],
								department_id: this.finddpt(id)[value],
							})
						}
						for (let c=0;c<department_list.length;c++){
							if (department_list[c].hasOwnProperty('department_id')&&department_list[c].hasOwnProperty('name')){
								this.finddpt(id).sed_department = department_list;
								break;
							}else {
								this.finddpt(id).sed_department.push({
									name: department_list[c][lable],
									department_id: department_list[c][value],
								})
							}
						}
					}

				}, (error)=> {
					this.$message.error('加载科室列表失败');
				});
			},
			finddpt(id){
				for (let i = 0; i < this.departments.length; i ++) {
					let item = this.departments[i];
					if (id == item[this.firstParams]) {
						return item;
					}
				}
				return null;
			},

		},
		created(){
			this.getdepartmentList();
		}
	}
</script>